package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Academy;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.AcademyMapper;
import com.example.server.web.service.AcademyService;
import com.example.server.web.utils.UUIDKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-19
 */
@Service
public class AcademyServiceImpl extends ServiceImpl<AcademyMapper, Academy> implements AcademyService {

    @Autowired
    private AcademyMapper academyMapper;

    @Override
    public Integer add(Academy academy) {
        addCheck(academy);
        if (StringUtils.isBlank(academy.getCode())){
            academy.setCode(UUIDKeyGenerator.generateCode("AD",true));
        }
        return academyMapper.insert(academy);
    }

    @Override
    public Map queryByPage(PageQueryDto dto) {
        HashMap<String, Object> map = new HashMap<>();
        //模糊查询
        LambdaQueryWrapper<Academy> wrapper = new LambdaQueryWrapper<>();
        String academyName = (String) dto.getParams("academyName");
        wrapper.like(StringUtils.isNotBlank(academyName),Academy::getAcademyName,academyName);
        //查询分页信息
        Page<Academy> page = this.page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);

        //封装map结果
        map.put("total",page.getTotal());
        map.put("academyList",page.getRecords());
        return map;
    }

    @Override
    public Boolean modify(Academy academy) {
        //判断修改的学院名字是否会重复
        LambdaQueryWrapper<Academy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Academy::getAcademyName,academy.getAcademyName());
        Academy one = academyMapper.selectOne(wrapper);
        if (one!=null&&one.getId()!= academy.getId()){
            throw new CustomException("该学院名已存在!");
        }
        return this.updateById(academy);
    }

    @Override
    public Boolean deletes(List<Integer> ids) {
        if(ids == null){
            throw new CustomException("请选择要删除的学院");
        }
        int i = academyMapper.deleteBatchIds(ids);
        if(i == ids.size()){
            return true;
        }
        return false;
    }

    /**
     * 新增之前校验填写的信息
     * @param academy
     */
    private void addCheck(Academy academy) {
        //查询是否存在重复名字
        LambdaQueryWrapper<Academy> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Academy::getAcademyName, academy.getAcademyName());
        Academy one = academyMapper.selectOne(wrapper1);
        if(one!=null){
            throw  new CustomException("该学院名字已存在，请重新换一个！");
        }
        if (StringUtils.isBlank(academy.getCode())) return;
        //查询是否存在重复学院代码
        LambdaQueryWrapper<Academy> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Academy::getCode, academy.getCode());
        Academy two = academyMapper.selectOne(wrapper2);
        if(two!=null){
            throw  new CustomException("该学院代码已存在，请重新换一个！");
        }
    }
}
